package com.tangcco.lockwell.controller.admin;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.tangcco.lockwell.base.RestResponse;
import com.tangcco.lockwell.service.SubjectService;
import com.tangcco.lockwell.service.impl.SubjectServiceImpl;
import com.tangcco.lockwell.viewmodel.admin.subject.SubjectVM;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.security.auth.Subject;
import javax.validation.Valid;
import java.util.List;
/**
 * author: 宋鑫垚
 * **/
@RestController("SubjectController")
@RequestMapping(value = "/api/admin/education")
public class SubjectController{
    @Resource
    private final SubjectServiceImpl subjectService;

    public SubjectController(SubjectServiceImpl subjectService) {
        this.subjectService = subjectService;
    }


    // 获取所有主要科目
    @RequestMapping(value = "/subject/main", method = RequestMethod.POST)
    public RestResponse<List<SubjectVM>> MainList() {
        //调用service的allSubject方法获取所有科目
        List<SubjectVM> subjects = subjectService.selectAllMainSubject();
        return RestResponse.ok(subjects);
    }
    //获取主要科目所对应的所有副科目 需要传过来一个父节点Id
    @RequestMapping(value = "/subject/list/{parentId}", method = RequestMethod.POST)
    public RestResponse<List<SubjectVM>> list(@PathVariable Integer parentId) {
        List<SubjectVM> subjectVMS = subjectService.selectAllSubject(parentId);
        return RestResponse.ok(subjectVMS);
    }
    // 分页获取科目   学科分页
    @RequestMapping(value = "/subject/page", method = RequestMethod.POST)
    public RestResponse<PageInfo<SubjectVM>> pageList(@RequestBody SubjectVM model) {
//        PageMethod pagehelper = new PageHelper();
        PageHelper pageHelper = new PageHelper();
        pageHelper.startPage(model.getPageIndex(), model.getPageSize());
        List<SubjectVM> subjectVMS = subjectService.selectSubjectByPage(model);
        PageInfo<SubjectVM> pageInfo = new PageInfo<>(subjectVMS);
        return RestResponse.ok(pageInfo);
    }
    // 添加或修改科目
    @RequestMapping(value = "/subject/edit", method = RequestMethod.POST)
    public RestResponse edit(@RequestBody @Valid SubjectVM model) {
        if (model.getId() == null) {
            subjectService.insertByFilter(model);
        } else {
            subjectService.updateByIdFilter(model);
        }
        return RestResponse.ok();
    }
    //学科详情查询
    @RequestMapping(value = "/subject/select/{id}", method = RequestMethod.POST)
    public RestResponse<SubjectVM> select(@PathVariable Integer id) {
        SubjectVM vm = subjectService.selectSubjectById(id);
        return RestResponse.ok(vm);
    }
    //删除学科
    @RequestMapping(value = "/subject/delete/{id}", method = RequestMethod.POST)
    public RestResponse delete(@PathVariable Integer id) {
        subjectService.deleteById(id);
        return RestResponse.ok();
    }
    //获取所有学科
    @RequestMapping(value = "/subject/list", method = RequestMethod.POST)
    public RestResponse<List<SubjectVM>> all() {
        List<SubjectVM> subjectVMS = subjectService.selectAll();
        return RestResponse.ok(subjectVMS);
    }

    //获取一级学科
    @PostMapping("/subject/level1")
    public RestResponse<List<SubjectVM>> leve1() {
        List<SubjectVM> subjectVMS = subjectService.selectLelevl1();
        return RestResponse.ok(subjectVMS);
    }
}
